<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" destroy-on-close :before-close="closeDailog"
        :close-on-click-modal="false" :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="120px">
            <el-form-item label="所属集团" prop="pkGroup">
                <ec-group-tree-select v-model="dataForm.pkGroup" placeholder="请选择所属集团">
                </ec-group-tree-select>
            </el-form-item>
            <el-form-item label="所属组织" prop="pkOrg">
                <ec-org-tree-select :pkGroup="dataForm.pkGroup" v-model="dataForm.pkOrg" placeholder="请选择所属组织">
                </ec-org-tree-select>
            </el-form-item>
            <el-form-item label="上级用户组" prop="pid">
                <ec-usergroup-tree-select :pkGroup="dataForm.pkGroup" :pkOrg="dataForm.pkOrg" v-model="dataForm.pid"
                    placeholder="请选择上级用户组">
                </ec-usergroup-tree-select>
            </el-form-item>
            <el-form-item label="用户组名称" prop="usergroupName">
                <el-input v-model="dataForm.usergroupName" placeholder="用户组名称"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as usergroupService from '@/api/upms/usergroup'
    import * as orgService from '@/api/upms/org'
    import * as groupService from '@/api/upms/group'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkUsergroup: 0,
                    pkGroup: '',
                    pkOrg: '',
                    pid: '',
                    usergroupName: '',
                    revision: '',
                },
                dataRule: {
                    pkGroup: [{
                        required: true,
                        message: '所属集团不能为空',
                        trigger: 'change'
                    }],
                    pkOrg: [{
                        required: true,
                        message: '所属组织不能为空',
                        trigger: 'change'
                    }],
                    pid: [{
                        required: false,
                        message: '上级用户组不能为空',
                        trigger: 'change'
                    }],
                    usergroupName: [{
                        required: true,
                        message: '用户组名称不能为空',
                        trigger: 'blur'
                    }]
                },
                // usergroupProps: {
                //     parent: 'pid',
                //     value: 'pkUsergroup',
                //     label: 'usergroupName',
                //     children: 'children',
                // },
                // usergroupTree: [],
            }
        },
        methods: {
            init(id) {
                // this.getDict();
                this.dataForm.pkUsergroup = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkUsergroup) {
                        usergroupService.getusergroupinfo(this.dataForm.pkUsergroup).then(data => {
                            this.dataForm.pkGroup = data.pkGroup
                            this.dataForm.pkOrg = data.pkOrg
                            this.dataForm.pid = data.pid
                            this.dataForm.usergroupName = data.usergroupName
                            this.dataForm.revision = data.revision
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkUsergroup': this.dataForm.pkUsergroup || undefined,
                            'pkGroup': this.dataForm.pkGroup,
                            'pkOrg': this.dataForm.pkOrg,
                            'pid': this.dataForm.pid,
                            'usergroupName': this.dataForm.usergroupName,
                            'revision': this.dataForm.revision,
                        }
                        var tick = !this.dataForm.pkUsergroup ? usergroupService.addusergroup(params) :
                            usergroupService.updateusergroup(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
            // getDict() {
            //     usergroupService.getusergrouptree(null).then(data => {
            //         this.usergroupTree = data;
            //     });
            // },
            // usergroupTreeChange(value) {
            //     this.dataForm.pid = value;
            // }
        }
    }
</script>